package com.shoukaiseki.xutils.simple;

import java.text.SimpleDateFormat;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;
import com.google.code.microlog4android.config.PropertyConfigurator;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


public class Test005Activity extends Activity{
	
	private static final Logger logger = LoggerFactory.getLogger(Test005Activity.class);
	
	int i=0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		PropertyConfigurator.getConfigurator(this).configure();
		super.onCreate(savedInstanceState);
		final LinearLayout layout2test005=new LinearLayout(this);  
		layout2test005.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams lp_LinearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		 lp_LinearLayout.gravity = Gravity.CENTER;
		final TextView textViewtest005 = new TextView(this);
		textViewtest005.setText("test json+http");
		layout2test005.addView(textViewtest005,lp_LinearLayout);
		
		lp_LinearLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		 lp_LinearLayout.gravity = Gravity.LEFT;
		final Button buttontest005=new Button(this);
		layout2test005.addView(buttontest005,lp_LinearLayout);
		buttontest005.setText("按钮1");
		
		buttontest005.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String str="test log按钮按下总计:"+(++i);
				textViewtest005.setText(str);
				HttpUtils http = new HttpUtils();
				// 设置缓存1ms，1ms内直接返回上次成功请求的结果。
				http.configCurrentHttpCacheExpiry(1);
				http.send(HttpRequest.HttpMethod.GET,
				    "http://maximodecryption.duapp.com/android/testandroid.jsp",
				    new RequestCallBack<String>(){
				        @Override
				        public void onLoading(long total, long current, boolean isUploading) {
				        	String str="test log按钮按下总计:"+(++i);
				        	str+="\n"+current + "/" + total;
				        	textViewtest005.setText(str);
				        }

				        @Override
				        public void onSuccess(ResponseInfo<String> responseInfo) {
				        	logger.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date())+""+responseInfo.result);
				            textViewtest005.setText("upload response:" +responseInfo.result);
				            try {
				            	JSONTokener jsonParser=new JSONTokener(responseInfo.result);
				            	
				            	 // 此时还未读取任何json文本，直接读取就是一个JSONObject对象。  
				            	JSONObject json = (JSONObject) jsonParser.nextValue();  
				            	String string = json.getString("JSON");
				            	textViewtest005.setText(textViewtest005.getText()+"\n"+string);
							} catch (JSONException ex) {
								// TODO: handle exception
				            	textViewtest005.setText(textViewtest005.getText()+"\n"+"获取数据失败");
							}
				        }

				        @Override
				        public void onStart() {
				        	textViewtest005.setText("conn...");
				        }

				        @Override
				        public void onFailure(HttpException error, String msg) {
				        	textViewtest005.setText("onFailure:"+msg);
				        }
				});
				
				
			}
		});
		setContentView(layout2test005);
	}	

}
